package com.sne.airways.basecore.dao;

import com.sne.airways.basecore.pojo.BaseProperties;
import com.sne.airways.system.utils.AppUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.util.Date;


/**
 * Created by IntelliJ IDEA.
 * User: 卞京帅{bjslogin#gmail.com}
 * Date: 12-11-11
 * Time: 下午5:26
 * To change this template use File | Settings | File Templates.
 */
public class DBHelper {

    public static final String[] IGNOREPROPERTIES = {"createDate","createUser","modifyDate","modifyUser"};

    public static void assembleQuery(Query query,Object... param){
        int index = 0;
        for(int i = 0 ; i < param.length ; i += 3){
            if(param[i + 2] != null && StringUtils.isNotBlank(param[i + 2].toString())){
                if(param[ i + 2 ] instanceof String){
                    query.setString(index,param[i + 2].toString());
                }else if(param[ i + 2 ] instanceof Integer){
                    query.setInteger(index,(Integer)param[i + 2]);
                }else if(param[ i + 2 ] instanceof Date){
                    query.setTimestamp(index,(Date)param[i + 2]);
                }else if(param[ i + 2 ] instanceof Boolean){
                    query.setBoolean(index,(Boolean)param[i + 2]);
                }else{
                    throw new IllegalArgumentException("不支持的参数类型!");
                }
                index ++;
            }
        }
    }

    public static void setBaseProperties(BaseProperties object){
        object.setCreateDate(new Date());
        object.setCreateUser(AppUtils.getCurrentUser());
        object.setModifyDate(new Date());
        object.setModifyUser(AppUtils.getCurrentUser());
        object.setVersion(0);
    }
}
